/*****************
* Ant Scriptdef/Javascript routine for installing frameworks
* or frameworks subsets into a directory
*
* @version 0.1
* @author Arnoud ten Hoedt / Roonaan
*********************/

// Import Java Classes
importClass(java.io.File);
importClass(java.lang.System);
importClass(java.net.URL);
importClass(java.io.FileWriter);

// Attribute fetching
var dir  = attributes.get("dir"),
    name = attributes.get("name");
if(!name) {
	self.fail("No name specified");
}
// Internal processing variables
var fDir = new File(dir);

// Testing dependencies
var sDep  = project.getProperty("ant4web.frameworks." + name + ".dep");
if(sDep == null) {
	self.fail("No dependency (ant4web.frameworks." + name + ".dep) defined while trying to install framework " + name);
}

// Resolving dependencies
sDepDir = resolveDep(sDep);

// Make sure the target directory exists
if(!fDir.exists()) {
	fDir.mkdirs();
}

// See if we need to use a subdir to copy files from
var sDepSub = project.getProperty("ant4web.frameworks." + name  + ".dir");

if(sDepSub == null) {
	var dFrom = new File(sDepDir);
} else {
	var dFrom = new File(sDepDir + "/" + sDepSub);
}

// Prepare for copying

var task = project.createTask("copy");
task.setTodir(fDir);

var excludes = ["**/ant.imported.log"];
var sExcludes = project.getProperty("ant4web.frameworks." + name + ".excludes");
if(sExcludes != null) {
	excludes.push(sExcludes);
}


var fs = project.createDataType("fileset");
	fs.setDir(dFrom);
	fs.setIncludes("**/*.*");
	fs.setExcludes(excludes.join(" "));
task.addFileset(fs);
task.execute();

// Utility function
function resolveDep(sDep) {
	var sDepUrl       = project.getProperty("ant4web.frameworks.deps." + sDep + ".url");
	var sDepMode      = project.getProperty("ant4web.frameworks.deps." + sDep + ".mode");
	var sDepExpand    = project.getProperty("ant4web.frameworks.deps." + sDep + ".expand");
	var sDepExpandBase= project.getProperty("ant4web.frameworks.deps." + sDep + ".expandbase");
	var sDepType      = project.getProperty("ant4web.frameworks.deps." + sDep + ".type");
	var sDepDir       = project.getProperty("ant4web.lib") + "/lib-ext/frameworks/" + sDep.replace('.','/');
	
	var task = project.createTask("ant4web.dependencies");
	task.setDynamicAttribute("dir", sDepDir);
	task.setDynamicAttribute("url", sDepUrl);
	task.setDynamicAttribute("type", sDepType);
	task.setDynamicAttribute("expand", sDepExpand);
	task.setDynamicAttribute("expandbase", sDepExpandBase);
	task.execute();
	
	/* See if there is any licensing information */
			
	var licProp = "ant4web.frameworks.deps." + sDep + ".license"
	var lic = project.getProperty(licProp);
	
	self.log(licProp + ": " + lic);
	
	if(lic && lic.length() > 0) {
		self.log("The ant4web.frameworks.deps." + sDep + " dependency is subject to the following licenses:");
		var lics = lic.split(",");
		for(var i = 0; i < lics.length; i++) {
			var licEcho = project.createTask("ant4web.license.echo");
			licEcho.setDynamicAttribute("license", lics[i]);
			licEcho.execute();
		}
	}
	
	return sDepDir;
}